﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Model.Jihuabu;
using Enterprise.Service.Jihuabu;
using Enterprise.Infrastructure;

namespace Enterprise.UI.Web.Modules.Jihuabu
{
    public partial class M01_XMB : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGrid();
            }
        }

        private void bindGrid()
        {
            string strwhere = " afebh is not null ";
            if (!string.IsNullOrEmpty(s_afkbh.Text))
            {
                strwhere += " and afebh='" + s_afkbh.Text + "'";
            }
            if (!string.IsNullOrEmpty(s_zt.Text))
            {
                strwhere += " and zt=" + s_zt.SelectedValue;
            }
            M01XMBService xmbservice = new M01XMBService();
            gv.DataSource = xmbservice.GetList(strwhere);
            gv.DataBind();
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            M01_XMBEntity model = new M01_XMBEntity();
            M01XMBService xmbservice = new M01XMBService();
            model.AFEBH = t_afebh.Text.Trim();
            model.AFEPZ_JG = t_afepz_jg.Text.ToDecimal();
            model.AFEPZ_MY = t_afepz_my.Text.ToDecimal();
            model.KGSJ = t_kgsj.Text;
            model.LRR = Page.User.Identity.Name;

            model.WGSJ = t_wgsj.Text;
            model.XMFL = t_xmfls.SelectedValue;
            model.XMMC = t_xmmc.Text;
            model.XMSSGS = t_ssdw.Text;
            model.ZT = t_xmzt.SelectedValue;
            if (!xmbservice.Exists(model.AFEBH)&&model.AFEBH!="")
            {
                xmbservice.Add(model);
            }
            else
                Utility.ShowMsg(this.Page, "添加失败！["+model.AFEBH+"]AFE编号已经存在或者不能为空");
            bindGrid();
        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            M01_XMBEntity model = new M01_XMBEntity();
            M01XMBService xmbservice = new M01XMBService();
            if (!string.IsNullOrEmpty(h_afebh.Value))
            {
                xmbservice.Delete(h_afebh.Value);
            }
            else
                Utility.ShowMsg(this.Page, "删除失败！请点击[修改]以选中之后再删除");
            bindGrid();
        }

        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            //保存
            M01_XMBEntity model = new M01_XMBEntity();
            M01XMBService xmbservice = new M01XMBService();
            //保存时选用旧编号
            model.AFEBH = h_afebh.Value;
            model.AFEPZ_JG = t_afepz_jg.Text.ToDecimal();
            model.AFEPZ_MY = t_afepz_my.Text.ToDecimal();
            model.KGSJ = t_kgsj.Text;
            model.LRR = Page.User.Identity.Name;

            model.WGSJ = t_wgsj.Text;
            model.XMFL = t_xmfls.SelectedValue;
            model.XMMC = t_xmmc.Text;
            model.XMSSGS = t_ssdw.Text;
            model.ZT = t_xmzt.SelectedValue;

            //如果编号发生变更
            if (h_afebh.Value != t_afebh.Text)
            {
                bool r = xmbservice.Change(model, t_afebh.Text);
                if (!r)
                {
                    Utility.ShowMsg(this.Page, "更新失败", "！请检查输入的AFE号码是否已经存在！","M01_XMB.aspx");
                }                
            }
            else
            {
                xmbservice.Update(model);
            }
            bindGrid();
            //Response.Redirect("M01_XMB.aspx",true);
        }

        //点击修改时加载数据到页面
        protected void gv_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            M01_XMBEntity model = new M01_XMBEntity();
            M01XMBService xbmservice = new M01XMBService();
            string selected_afebh = gv.SelectedRow.Cells[0].Text;
            model = xbmservice.GetModel(selected_afebh);            
            if (!string.IsNullOrEmpty(model.AFEBH))
            {
                h_afebh.Value = model.AFEBH;
                t_afebh.Text = model.AFEBH;
                t_afepz_jg.Text = model.AFEPZ_JG.ToDecimal().ToString();
                t_afepz_my.Text = model.AFEPZ_MY.ToDecimal().ToString();
                t_kgsj.Text = model.KGSJ;
                t_ssdw.Text = model.XMSSGS;
                t_wgsj.Text = model.WGSJ;
                t_xmfls.SelectedValue = model.XMFL;
                t_xmmc.Text = model.XMMC;
                if (string.IsNullOrEmpty(model.ZT))
                    t_xmzt.SelectedIndex = -1;
                else
                    t_xmzt.SelectedValue = model.ZT;
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            bindGrid();
        }
    }
}